<template>
  <div class="table-section">
    <div class="table-section_tool">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="settleStatus">
          <DictSelect dict="data_card_optype" v-model="queryParams.opType" placeholder="操作类型" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <vxe-grid ref="tableRef" v-bind="gridOptions">
      <template #optype="{ row }">
        <div class="dl-flex">
          <dict-tag :options="data_card_optype" :value="row.opType" />
          <span v-if="row.opType == 3">（延期至{{ row.expireTime ? row.expireTime.substring(0, 10) : '-' }}）</span>
        </div>
      </template>
      <template #deviceType="{ row }">
        <dict-tag :options="station_device_type" :value="row.deviceType" />
      </template>

      <template #bottom>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
import { getDataCardHistory } from '@/api/station/dataCard/index';
import { getTableConfig } from './columns';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps({
  cardId: [String, Number]
});
const { data_card_optype, station_device_type } = toRefs<any>(proxy?.useDict('data_card_optype', 'station_device_type'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  opType: '',
  stationName: '',
  regionCode: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await getDataCardHistory({ ...queryParams.value, cardId: props.cardId });
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

onMounted(() => {
  getList();
});
</script>
